using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;namespace WebApplication1{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void BtnUpload_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { string fileName = FileUpload1.FileName;//获取上传的文件名 this.TextBox1.Text = fileName; //获取上传文件的后缀名 String filefix = fileName.Substring(fileName.LastIndexOf('.') 1).ToLower(); if (filefix!="png"&&filefix!="jpg"&&filefix!="jpeg"&&filefix!="gif") { FileUpload1.SaveAs(Server.MapPath(".") "//UploadPic//" fileName); this.ImgPic.ImageUrl = ""; this.lblMessage.Text = "文件上传成功!"; } else { FileUpload1.SaveAs(Server.MapPath(".") "//UploadPic//" fileName); this.ImgPic.ImageUrl = "~/UploadPic/" fileName; this.lblMessage.Text = "图片上传成功!"; } } } protected void btnDownload_Click(object sender, EventArgs e) { string name = this.TextBox1.Text; //MessageBox.Show(name); string filename = Server.MapPath(".") "/UploadPic/" name; FileInfo fileinfo = new FileInfo(filename); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Disposition", "attachment;filename=" Server.UrlEncode(fileinfo.Name)); Response.AddHeader("Content-Length", fileinfo.Length.ToString()); Response.ContentType = "application/x-bittorrent"; Response.WriteFile(fileinfo.FullName); Response.End(); //Response.ContentType = "application/x-zip-compressed"; //Response.AddHeader("Content-Disposition", "attachment;filename=" name); //Response.TransmitFile(filename); } }}
评论